/*******************************************************************************
 * 
 ******************************************************************************/
var updatePriceWin;
var searchPriceWin;
var priceUpdateForm;
var priceGrid;
var p_l_store;
var p_s_store;
var p_cm;

var pageSize = 5;

Ext.onReady(function() {
	/*
	 * Ext.QuickTips.init();
	 * 
	 * Ext.form.Field.prototype.msgTarget = "side";
	 * 
	 * Ext.BLANK_IMAGE_URL =
	 * "http://localhost/extjstest/resources/images/default/s.gif";
	 */

	TopicRecord = Ext.data.Record.create([{
				name : 'id',
				mapping : 'commodity.id'
			}, {
				name : 'commodity_name',
				mapping : 'commodity.commodity_name'
			}, {
				name : 'category_name',
				mapping : 'commodity.category.category_name'
			}, {
				name : 'current_price',
				mapping : 'commodity.current_price_old'
			}]);

	p_cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			new Ext.grid.CheckboxSelectionModel(), {

				header : "编号",

				align : "center",

				dataIndex : "id"

			}, {

				header : "商品名",

				align : "center",

				dataIndex : "commodity_name"

			}, {

				header : "商品类型",

				dataIndex : "category_name",

				align : "center"

			}, {

				header : "商品价格",

				dataIndex : "current_price",

				align : "center"

			}]);

	p_l_store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : "priceAction!list.action"
						}),
				reader : new Ext.data.JsonReader({
							totalProperty : 'totalProperty',
							root : 'price_list',
							successProperty : 'SUCCESS',
							fields : [{
										name : 'id',
										mapping : 'id'
									}, {
										name : 'commodity_name',
										mapping : 'commodity_name'
									}, {
										name : 'category_name',
										mapping : 'category.category_name'
									}, {
										name : 'current_price',
										mapping : 'current_price',
										type : 'float'
									}]
						}),

				sortInfo : {
					field : 'id',
					direction : 'ASC'
				}

			});

	p_s_store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : "priceAction!search.action"
						}),
				reader : new Ext.data.JsonReader({
							totalProperty : 'totalProperty',
							root : 'price_list',
							successProperty : 'SUCCESS',
							fields : [{
										name : 'id',
										mapping : 'id'
									}, {
										name : 'commodity_name',
										mapping : 'commodity_name'
									}, {
										name : 'category_name',
										mapping : 'category.category_name'
									}, {
										name : 'current_price',
										mapping : 'current_price',
										type : 'float'
									}]
						}),

				sortInfo : {
					field : 'id',
					direction : 'ASC'
				}

			});
			
			
	priceUpdateForm = new Ext.form.FormPanel({
				url : "priceAction!update.action",
				method : 'post',
				defaultType : "textfield",
				labelWidth : 55,
				defaults : {
					anchor : "92%"
				},
				baseCls : "x-plain",
				items : [{
							xtype : "hidden",
							name : "commodity.id"
						},{
							fieldLabel : "商品名",
							name : "commodity.commodity_name",
							readOnly : "ture"
						},{
							fieldLabel : "当前价格",
							name : "commodity.current_price_old",
							readOnly : "ture"
						},{
							fieldLabel : "新价格",
							name : "commodity.current_price",
							allowBlank : false,
							editable : false
						}]

			});

	updatePriceWin = new Ext.Window({
				layout : 'fit',
				buttonAlign : 'center',
				width : 330,
				height : 300,
				closeAction : 'hide',
				modal : true,
				plain : true,
				buttons : [{
							text : '保存',
							handler : function() {
								if (priceUpdateForm.getForm().isValid()) {
									priceGrid.reconfigure(p_l_store, p_cm);
									priceUpdateForm.getForm().submit({
												success : function(form, action) {
													priceUpdateForm.getForm()
															.reset();
													updatePriceWin.hide();
													priceGrid.getStore()
															.removeAll();
													priceGrid.getStore().load({
																params : {
																	start : 0,
																	limit : pageSize
																}
															});
												},
												waitMsg : '保存数据...'
											});
								}
							}
						}, {
							text : '取消',
							handler : function() {
								priceUpdateForm.getForm().reset();
								updatePriceWin.hide();
							}
						}],
				items : [priceUpdateForm]
			});

	searchPriceWin = new Ext.Window({
				title : '查找商品',
				layout : 'form',
				width : 300,
				height : 180,
				closeAction : 'hide',
				buttonAlign : 'center',
				labelWidth : 55,
				modal : true,
				plain : true,

				buttons : [{
					text : '查找',
					handler : function() {
						priceGrid.getStore().removeAll();
						priceGrid.reconfigure(p_s_store, p_cm);
						//alert("name:" + Ext.getCmp("searchName").getValue());
						p_s_store.load({
									params : {
										start : 0,
										limit : pageSize,
										"commodity.id" : Ext.getCmp("searchId")
												.getValue(),
										"commodity.commodity_name" : Ext.getCmp("searchName")
												.getValue()
									}
								});
						searchPriceWin.hide();
						Ext.getCmp("searchId").setValue("");
						Ext.getCmp("searchName").setValue("");
					}

				}, {
					text : '取消',
					handler : function() {
						Ext.getCmp("searchId").setValue("");
						Ext.getCmp("searchName").setValue("");
						searchPriceWin.hide();
					}
				}],
				items : [{
							fieldLabel : '商品编号',
							width : 200,
							id : 'searchId',
							name : 'searchId',
							// allowBlank : false,
							xtype : 'textfield'

						}, {

							fieldLabel : '商品名称',
							id : 'searchName',
							name : 'searchName',
							width : 200,
							// allowBlank : false,
							xtype : 'textfield'

						}]

			});

	priceGrid = new Ext.grid.GridPanel({
		renderTo : 'gridPanel',
		store : p_l_store,
		cm : p_cm,
		autoHeight : true,
		title : '价格调整',
		frame : true,
		stripeRows : true,
		tbar : [{
					text : "查询商品",
					handler : function() {
						searchPriceWin.show();
					},
					scope : this
				}, "-", {
					text : "调整价格",
					handler : function() {
						var sm = priceGrid.getSelectionModel();
						if (sm.hasSelection()) {
							updatePriceWin.show();
							try {
								var data = priceGrid.getSelectionModel()
										.getSelected();
								var myNewRecord = new TopicRecord({
											'commodity.id' : data.get("id"),
											'commodity.commodity_name' : data.get("commodity_name"),
											'commodity.category.category_name' : data.get("category_name"),
											'commodity.current_price_old' : data.get("current_price")
										});
								priceUpdateForm.getForm()
										.loadRecord(myNewRecord);

							} catch (_err) {
								Ext.Msg.alert("系统提示", _err.description);
								updatePriceWin.hide();
							}
						} else {
							Ext.Msg.alert('信息', "请选中一行！");
						}
					},
					scope : this
				}],
		enableColumnMove : false,
		selModel : new Ext.grid.CheckboxSelectionModel(),
		bbar : new Ext.PagingToolbar({
					pageSize : pageSize,
					store : p_l_store
				})
	});
	
	priceGrid.show();
	
	priceGrid.getStore().load({
				params : {
					start : 0,
					limit : pageSize
				}
			});
			
});
